package com.thingple.web.bridge.reader;


import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;

import com.thingple.h5.bridge.BridgeConfig;
import com.thingple.settings.PreferencesUtils;
import com.thingple.uhf.DeviceSession;
import com.thingple.uhf.InventoryListener;
import com.thingple.uhf.RFIDDeviceManager;
import com.thingple.uhf.TagInfo;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * Created by lism on 2017/8/14.
 */
class InventoryHandler {

    /**
     * EPC为key,tid可能读不到
     */
    private Map<String, TagInfo> tagMap = new HashMap<>();

    private InventoryListener listener;

    private WebView webView;

    private Context context;

    private InventoryJsBridge jsBridge;

    private String filter;

    private int power;

    InventoryHandler(Context context, WebView webView, InventoryJsBridge jsBridge, String filter, int power) {

        this.context = context;
        this.webView = webView;
        this.jsBridge = jsBridge;
        this.filter = filter;
        this.power = power;
        this.listener = initListener();

        start();
    }

    private void start() {
        DeviceSession session = DeviceSession.newInstance(context);
        Bundle bundle = new Bundle();
        if (filter != null) {
            bundle.putString("filter", filter);
        }
        if (power > 0) {
            bundle.putInt("power", power);
        } else {
            bundle.putInt("power", PreferencesUtils.getHighPower());
        }
        RFIDDeviceManager.shareInstance().startIventoryAsync(context, session, listener, bundle);
    }

    private void callback(String tid, String epc, String user) {

        BridgeConfig bridgeConfig = jsBridge.getClass().getAnnotation(BridgeConfig.class);
        if (bridgeConfig == null) {
            return;
        }
        if (tid == null) {
            tid = "";
        }
        if (epc == null) {
            epc = "";
        }
        if (user == null) {
            user = "";
        }

        String module = bridgeConfig.moduleName();
        String funcName = module + "_listener.onInventory";
        String callbackContent = "javascript:(function() {" + "if (" + module + " && " + funcName + ") {" +
                funcName + "('" + tid + "', '" + epc + "', '" + user + "');" +
                "}" +
                "})();";
        this.webView.loadUrl(callbackContent);
    }

    private InventoryListener initListener() {
        return new InventoryListener() {

            @Override
            public void onInventory(String tid, String epc, String user) {
                TagInfo tag = tagMap.get(epc);

                if (tag == null) {// 新读到
                    tag = new TagInfo();
                    tag.tid = tid;
                    tag.epc = epc;
                    tag.user = user;
                    tagMap.put(epc, tag);
                    callback(tid, epc, user);
                } else {
                    // 已经读过的数据
                    Log.d(getClass().getName() + "#onInventory", "重复数据,忽略");
                }
            }
        };
    }

    void destroy() {
        DeviceSession session = DeviceSession.currentInstance();
        if (session != null) {
            Log.d(getClass().getName() + "#stopInventort", "停止Inventory");
            RFIDDeviceManager.shareInstance().stopInventory(session);
        } else {
            //
            Log.d(getClass().getName() + "#stopInventort", "无Inventory session, 不需要释放资源");
        }
    }
}
